import torch

# ======== 余弦核函数(注意力机制) ==========
a = torch.tensor([[1, 2, 4]])
b = torch.tensor([[1, 2, 3]])


def cosine_similarity(a, b): # -1 ~ 1
    return (a @ b.T) / ((a @ a.T) ** 0.5 * (b @ b.T) ** 0.5)


print(cosine_similarity(a, b))  # tensor([[0.9915]]) 向量的相似度99.15%
